home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / GetPPPStatus / AEHelpers.c next >
Encoding:
Text File  |  2000-09-28  |  13.4 KB  |  527 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        AEHelpers.c
  3.  
  4.     Contains:    Functions to help you when you are building and sending Apple events.
  5.  
  6.     Written by: Andy Bachorski    
  7.  
  8.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/22/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23. //    Constant used to #undef PASCAL when not compiling a library
  24. #define    COMPILING_MORE_FINDER_EVENTS    true
  25.  
  26. //    System includes
  27. #include <AERegistry.h>
  28. #include <AEObjects.h>
  29. #include <AEPackObject.h>
  30. #include <Aliases.h>
  31. #include <Gestalt.h>
  32. #include <Icons.h>
  33.  
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36.  
  37. //    MoreFinderEvents includes
  38. #include "FinderRegistry.h"
  39. #include "MoreFinderEvents.h"
  40. #include "TrapUtils.h"
  41.  
  42. //    Export symbols in this header for shared libraries
  43. #pragma export on
  44. #include "AEHelpers.h"
  45. #pragma export off
  46.  
  47.  
  48. // *****************************************************************************
  49. //    Private Prototypes
  50. // *****************************************************************************
  51.  
  52. pascal    OSErr    MyIconAction (ResType theIconType,
  53.                            Handle *theIcon,
  54.                            void *myDataPtr);
  55.                            
  56. /*
  57.     Used by AEHMakeIconFamilyRecord, passed to ForEachIconDo as the IconAction
  58.     function. Puts each icon in an icon suite into the descriptor record passed
  59.     in the myDataPtr parameter.
  60. */
  61.  
  62. // ************************************************************************************************
  63.  
  64. pascal    OSErr    FindProcessBySignature( const OSType targetType,
  65.                                         const OSType targetCreator,
  66.                                         ProcessSerialNumberPtr psnPtr )
  67. {
  68.     OSErr        anErr = noErr;
  69.     Boolean        foundTheProcess = false;
  70.     
  71.     ProcessInfoRec    infoRec;
  72.     
  73.     infoRec.processInfoLength = sizeof( ProcessInfoRec );
  74.     infoRec.processName = nil;
  75.     infoRec.processLocation = nil;
  76.     infoRec.processAppSpec = nil;
  77.     
  78.     psnPtr->lowLongOfPSN = kNoProcess;
  79.     psnPtr->highLongOfPSN = kNoProcess;
  80.  
  81.     while ( !foundTheProcess )
  82.     {
  83.         anErr = GetNextProcess( psnPtr );
  84.         if ( anErr == noErr )
  85.         {
  86.             anErr = GetProcessInformation( psnPtr, &infoRec );
  87.             if ( ( anErr == noErr )
  88.                  && ( infoRec.processType == targetType )
  89.                  && ( infoRec.processSignature == targetCreator ) )
  90.             {
  91.                 foundTheProcess = true;
  92.             }
  93.         }
  94.     }
  95.     
  96.     return anErr;
  97.  
  98. }//end FindProcessBySignature
  99.  
  100. // ************************************************************************************************
  101.  
  102. pascal    OSErr    AEHMakeAppleEventSignatureTarget( const OSType targetType,
  103.                                                   const OSType targetCreator,
  104.                                                   const AEEventClass eventClass,
  105.                                                   const AEEventID eventID,
  106.                                                         AppleEvent *theEvent )
  107. {
  108.     OSErr    anErr = noErr;
  109.     
  110.     ProcessSerialNumber        psn = { kNoProcess, kNoProcess };
  111.     
  112.     anErr = FindProcessBySignature( targetType, targetCreator, &psn );
  113.     if ( anErr == noErr )
  114.     {
  115.         anErr = AEHMakeEventProcessTarget( &psn, eventClass, eventID, theEvent );
  116.     }
  117.     return anErr;
  118. }//end AEHMakeAppleEventSignatureTarget
  119.  
  120. // ************************************************************************************************
  121.  
  122. pascal    OSErr    AEHMakeEventProcessTarget( const ProcessSerialNumberPtr psnPtr,
  123.                                            const AEEventClass eventClass,
  124.                                            const AEEventID eventID,
  125.                                                  AppleEvent *theEvent )
  126. {
  127.     OSErr    anErr = noErr;
  128.     AEDesc    targetAppDesc = { typeNull, nil };
  129.     
  130.     anErr = AECreateDesc (typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &targetAppDesc);
  131.  
  132.     if ( anErr == noErr )
  133.     {
  134.         anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
  135.                                     kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  136.     }
  137.     
  138.     AEDisposeDesc( &targetAppDesc );
  139.     
  140.     return anErr;
  141. }//end AEHMakeEventProcessTarget
  142.  
  143. // ************************************************************************************************
  144.  
  145. pascal    OSErr    AEHMakeEventTargetID( const TargetID *targetIDPtr,
  146.                                       const AEEventClass eventClass,
  147.                                       const AEEventID eventID,
  148.                                             AppleEvent *theEvent )
  149. {
  150.     OSErr    anErr = noErr;
  151.     AEDesc    targetAppDesc = { typeNull, nil };
  152.     
  153.     anErr = AECreateDesc (typeTargetID, targetIDPtr, sizeof( TargetID ), &targetAppDesc);
  154.  
  155.     if ( anErr == noErr )
  156.     {
  157.         anErr = AECreateAppleEvent( eventClass, eventID, &targetAppDesc,
  158.                                     kAutoGenerateReturnID, kAnyTransactionID, theEvent);
  159.     }
  160.     
  161.     AEDisposeDesc( &targetAppDesc );
  162.     
  163.     return anErr;
  164. }//end AEHMakeEventProcessTarget
  165.  
  166. // ************************************************************************************************
  167.  
  168. pascal    OSErr    AEHMakeAliasDescFromFSSpec( const FSSpecPtr fssPtr,
  169.                                             AEDesc *aliasDesc )
  170. {
  171.     OSErr            anErr = noErr;
  172.     AliasHandle        aliasHandle;
  173.     
  174.     NewAlias( nil, fssPtr, &aliasHandle);
  175.  
  176.     if ( aliasHandle == nil )
  177.     {
  178.         anErr = paramErr;
  179.     }
  180.     else
  181.     {
  182.         anErr = AEHMakeAliasDesc( aliasHandle, aliasDesc );
  183.         DisposeHandle( (Handle)aliasHandle );
  184.     }
  185.         
  186.     return anErr;
  187. }//end MakeAliasObject
  188.  
  189. // ************************************************************************************************
  190.  
  191. pascal    OSErr    AEHMakeAliasDesc( const AliasHandle aliasHandle,
  192.                                         AEDesc *aliasDesc )
  193. {
  194.     OSErr    anErr = noErr;
  195.     
  196.     char    handleState = HGetState( (Handle)aliasHandle );
  197.     HLock( (Handle)aliasHandle );
  198.     
  199.     anErr = AECreateDesc( typeAlias, *aliasHandle, GetHandleSize( (Handle)aliasHandle ), aliasDesc );
  200.     
  201.     HSetState( (Handle)aliasHandle, handleState );
  202.     
  203.     return anErr;
  204. }//end MakeAliasObject
  205.  
  206. // ************************************************************************************************
  207.  
  208. pascal    OSErr    AEHMakeAliasObjectFromFSSpec( const FSSpecPtr fssPtr,
  209.                                                     AEDesc *containerObj,
  210.                                                     AEDesc *aliasObject )
  211. {
  212.     OSErr            anErr = noErr;
  213.     AliasHandle        aliasHandle;
  214.     
  215.     anErr = NewAlias( nil, fssPtr, &aliasHandle);
  216.     if ( aliasHandle == nil )
  217.     {
  218.         anErr = paramErr;
  219.     }
  220.     
  221.     if ( anErr == noErr )
  222.     {
  223.         anErr = AEHMakeAliasObject( aliasHandle, containerObj, aliasObject );
  224.     }
  225.     
  226.     DisposeHandle( (Handle)aliasHandle );
  227.     
  228.     return anErr;
  229. }//end MakeAliasObject
  230.  
  231. // ************************************************************************************************
  232.  
  233. pascal    OSErr    AEHMakeAliasObject( const AliasHandle aliasHandle,
  234.                                           AEDesc *containerObj,
  235.                                           AEDesc *aliasObject )
  236. {
  237.     OSErr    anErr = noErr;
  238.     AEDesc    aliasDesc;
  239.  
  240.     char    handleState = HGetState( (Handle)aliasHandle );
  241.     HLock( (Handle)aliasHandle );
  242.     
  243.     anErr = AECreateDesc( typeAlias, *aliasHandle, GetHandleSize( (Handle)aliasHandle ), &aliasDesc );
  244.     HSetState( (Handle)aliasHandle, handleState );
  245.     
  246.     if ( anErr == noErr )
  247.     {
  248.         anErr = CreateObjSpecifier( typeAlias, containerObj, formAbsolutePosition,
  249.                                     &aliasDesc, true, aliasObject );
  250.         AEDisposeDesc( &aliasDesc );
  251.     }
  252.     
  253.     return anErr;
  254. }//end MakeAliasObject
  255.  
  256. // ************************************************************************************************
  257.  
  258. pascal    OSErr    AEHMakePropertyObject( const DescType propType,
  259.                                              AEDesc *containerObj,
  260.                                              AEDesc *propertyObj )
  261. {
  262.     OSErr    anErr = noErr;
  263.     AEDesc    propDesc;
  264.     
  265.     anErr = AECreateDesc( typeType, &propType, sizeof( propType ), &propDesc );
  266.     
  267.     if ( anErr == noErr )
  268.     {
  269.         anErr = CreateObjSpecifier( cProperty, containerObj, formPropertyID,
  270.                                     &propDesc, true, propertyObj );
  271.         AEDisposeDesc( &propDesc );
  272.     }
  273.     
  274.     return anErr;
  275. }//end MakePropertyObject
  276.  
  277. // ************************************************************************************************
  278.  
  279. pascal    OSErr    AEHMakeProcessObject( const ProcessSerialNumber *psnPtr,
  280.                                              AEDesc *containerObj,
  281.                                              AEDesc *propertyObj )
  282. {
  283.     OSErr    anErr = noErr;
  284.     AEDesc    psnDesc;
  285.     
  286.     anErr = AECreateDesc( typeProcessSerialNumber, psnPtr, sizeof( ProcessSerialNumber ), &psnDesc );
  287.     
  288.     if ( anErr == noErr )
  289.     {
  290.         anErr = CreateObjSpecifier( cProperty, containerObj, formPropertyID,
  291.                                     &psnDesc, true, propertyObj );
  292.         AEDisposeDesc( &psnDesc );
  293.     }
  294.     
  295.     return anErr;
  296. }//end MakePropertyObject
  297.  
  298. // ************************************************************************************************
  299.  
  300. pascal    OSErr    AEHMakeSelectionObject( const DescType selection,
  301.                                               AEDesc *containerObj,
  302.                                               AEDesc *selectionObject )
  303. {
  304.     OSErr    anErr = noErr;
  305.     
  306.     AEDesc    selectionDesc = { typeNull, nil };
  307.     
  308.     anErr = AECreateDesc( typeAbsoluteOrdinal, &selection, sizeof( selection ), &selectionDesc );
  309.  
  310.     if ( anErr == noErr )
  311.     {
  312.         anErr = CreateObjSpecifier( cObject, containerObj, formAbsolutePosition,
  313.                                     &selectionDesc, true, selectionObject );
  314.     }            
  315.     return anErr;
  316. }
  317.  
  318. // ************************************************************************************************
  319.  
  320. pascal    OSErr    AEHMakeIconSuite( const AEDescList *iconFamilyRecPtr,
  321.                                         Handle *iconSuitePtr )
  322. {
  323.  
  324.     static    DescType    iconTypes[] = {    typeIconAndMask, type8BitIcon, type4BitIcon,
  325.                                         typeSmallIconAndMask, typeSmall8BitIcon, typeSmall4BitIcon };
  326.     const    long        iconTypesCnt = 6;
  327.     
  328.     OSErr    anErr = noErr;
  329.     
  330.     AEDescList    iconList = { typeNull, nil };
  331.     
  332.     anErr = AECoerceDesc( iconFamilyRecPtr, typeAERecord, &iconList );
  333.     
  334.     if ( anErr == noErr )
  335.     {
  336.         anErr = NewIconSuite( iconSuitePtr );
  337.         
  338.         if ( anErr == noErr )
  339.         {
  340.             long    index;
  341.             AEDesc    iconDataDesc = { typeNull, nil };
  342.             
  343.             for ( index = 0; index < iconTypesCnt; index++ )
  344.             {
  345.                 anErr = AEGetKeyDesc( &iconList, iconTypes[ index ],
  346.                                       typeWildCard, &iconDataDesc );
  347.                 if ( anErr == noErr )
  348.                 {
  349.                     anErr = AddIconToSuite( iconDataDesc.dataHandle,
  350.                                             *iconSuitePtr, iconTypes[ index ] );
  351.                 }
  352.             }
  353.         }
  354.     }
  355.     AEDisposeDesc( &iconList );
  356.  
  357.     return anErr;
  358. }
  359.  
  360. // ************************************************************************************************
  361.  
  362. pascal    OSErr    MyIconAction( ResType theIconType,
  363.                               Handle *theIcon,
  364.                               void *myDataPtr)
  365. {
  366.     OSErr    anErr = noErr;
  367.     
  368.     if ( *theIcon != nil )    // only add the icon if it's really there
  369.     {
  370.         AEDescList *iconFamilyRecPtr = (AEDescList*)myDataPtr;
  371.         
  372.         anErr = AEPutKeyPtr( iconFamilyRecPtr, theIconType, theIconType,
  373.                              **theIcon, GetHandleSize( *theIcon ) );
  374.     }
  375.         
  376.     return anErr;
  377. }//end MyIconAction
  378.  
  379. // ************************************************************************************************
  380.  
  381. pascal    OSErr    AEHMakeIconFamilyRecord( const Handle iconSuite,
  382.                                          const IconSelectorValue iconSelector,
  383.                                                AEDescList *iconFamilyRecPtr )
  384. {
  385.     OSErr    anErr = noErr;
  386.     AEDescList    iconList = { typeNull, nil };
  387.     
  388.     static    IconActionUPP iconActionUPP;
  389.     
  390.     if ( iconActionUPP == nil )
  391.     {
  392.         iconActionUPP = NewIconActionProc( &MyIconAction );
  393.     }
  394.     
  395.     // create a record for the icon family
  396.     anErr = AECreateList( nil, 0, true, &iconList );
  397.     
  398.     if ( anErr == noErr )
  399.     {
  400.         ForEachIconDo( iconSuite, iconSelector,
  401.                        iconActionUPP, &iconList );
  402.         
  403.         anErr = AECoerceDesc( &iconList, typeIconFamily, iconFamilyRecPtr );
  404.         AEDisposeDesc( &iconList );
  405.     }
  406.  
  407.     return anErr;
  408. }
  409.  
  410.  
  411.  
  412. // ************************************************************************************************
  413.  
  414. pascal    OSErr    AEHGetHandlerError( const AppleEvent *reply )
  415. {
  416.     OSErr        anErr = noErr;
  417.     
  418.     DescType    actualType;
  419.     long        actualSize;
  420.     long        handlerErr;
  421.     
  422.     if ( reply->descriptorType != typeNull )    // there's a reply, so there may be an error
  423.     {
  424.         OSErr    getErrErr = noErr;
  425.         
  426.         getErrErr = AEGetParamPtr( reply, keyErrorNumber, typeLongInteger, &actualType,
  427.                                     &handlerErr, sizeof( long ), &actualSize );
  428.         
  429.         if ( getErrErr != errAEDescNotFound )    // found an errorNumber parameter
  430.         {
  431.             anErr = handlerErr;                // so return it's value
  432.         }
  433.     }
  434.     return anErr;
  435. }//end AEHGetHandlerError
  436.  
  437. // ************************************************************************************************
  438.  
  439. pascal    Boolean    AEHSimpleIdleFunction( EventRecord *event,
  440.                                       long *sleepTime,
  441.                                       RgnHandle *mouseRgn )
  442. {
  443. #pragma unused( event )
  444.     *sleepTime = 30;
  445.     *mouseRgn = nil;
  446.     
  447.     return ( false );
  448. }//end AEHSimpleIdleFunction
  449.  
  450. // ********************************************************
  451.  
  452. pascal Boolean HasAppleEvents( void )
  453. {
  454.     OSErr    anErr = noErr;
  455.     
  456.     Boolean        hasAppleEvents = false;
  457.     
  458.     if ( TrapAvailable( _Gestalt ) )
  459.     {
  460.         long    response;
  461.         
  462.         if ( Gestalt( gestaltAppleEventsAttr, &response ) == noErr )
  463.         {
  464.             hasAppleEvents = ( response & (1L << gestaltAppleEventsPresent) );
  465.         }
  466.     }
  467.     else
  468.     {
  469.         hasAppleEvents = false;
  470.     }
  471.     
  472.     return hasAppleEvents ;
  473. }//end HasAppleEvents
  474.  
  475. // ********************************************************
  476.  
  477. pascal Boolean FinderCallsAEProcess( void )
  478. {
  479.     OSErr    anErr = noErr;
  480.     
  481.     Boolean        finderCallsAEProcess = false;
  482.     
  483.     if ( TrapAvailable( _Gestalt ) )
  484.     {
  485.         long    response;
  486.         
  487.         if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
  488.         {
  489.             finderCallsAEProcess = ( response & (1L << gestaltFinderCallsAEProcess) );
  490.         }
  491.     }
  492.     else
  493.     {
  494.         finderCallsAEProcess = false;
  495.     }
  496.     
  497.     return finderCallsAEProcess ;
  498. }//end FinderCallsAEProcess
  499.  
  500. // ********************************************************
  501.  
  502. pascal Boolean FinderIsOSLCompliant( void )
  503. {
  504.     OSErr    anErr = noErr;
  505.     
  506.     Boolean        finderIsOSLCompliant = false;
  507.     
  508.     if ( TrapAvailable( _Gestalt ) )
  509.     {
  510.         long    response;
  511.         
  512.         if ( Gestalt( gestaltFinderAttr, &response ) == noErr )
  513.         {
  514.             finderIsOSLCompliant = ( response & (1L << gestaltOSLCompliantFinder) );
  515.         }
  516.     }
  517.     else
  518.     {
  519.         finderIsOSLCompliant = false;
  520.     }
  521.     
  522.     return finderIsOSLCompliant ;
  523. }//end FinderIsOSLCompliant
  524.  
  525. // ********************************************************
  526.  
  527.